// pages/category/category.js
import { testrequest} from "../../request/index.js"
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList:[],
    rightContent:[],
    currentIndex:0,
    scrollTop:0
  },
// 接口返回数据
  Cates:[],

  onLoad: function (options) {
    //获取本地存储
    const Cates=wx.getStorageSync("cates");
    const spireTime=10000//过期时间
    if(!Cates || Date.now()-Cates.time>spireTime){
      this.getCates();
    }
    else{
      this.Cates=Cates.data;
      let leftMenuList=this.Cates.map(v=>v.cat_name);//map是遍历函数
     let rightContent=this.Cates[0].children;
     this.setData({leftMenuList,rightContent})
    }
    
  },

  // 获取商品分类数据
async getCates(){
   testrequest({url:"/categories"})
   .then(res=>{
     this.Cates=res;
     wx.setStorageSync("cates",{time:Date.now(),data:this.Cates})
     let leftMenuList=this.Cates.map(v=>v.cat_name);//map是遍历函数
     let rightContent=this.Cates[0].children;
     this.setData({leftMenuList,rightContent})
   })

  //使用es7的async await发送请求
  // const res = await testrequest({ url: "/categories" });
  // this.Cates = res;
  // wx.setStorageSync("cates", { time: Date.now(), data: this.Cates })
  // let leftMenuList = this.Cates.map(v => v.cat_name);//map是遍历函数
  // let rightContent = this.Cates[0].children;
  // this.setData({ leftMenuList, rightContent })
 },

//  左侧栏点击切换
handleItemTap(e){
  const {index}=e.currentTarget.dataset;
  let rightContent=this.Cates[index].children;
  this.setData({
    currentIndex:index,
    rightContent,
    scrollTop:0
  })

},
})